﻿''' <summary>
''' 锐剑系统 记账凭证库
''' 对应表- 记账凭证库
''' </summary>
Public Class RjClsVoucherDbAccess
    Inherits ClsBaseDbAccess

    Public Sub New(ByVal s As String)
        MyBase.New(s, TABLE_NAME_RJ_VOUCHER)
    End Sub

    Public Sub New(ByVal s As String, ByVal n As String)
        MyBase.New(s, n)
    End Sub

    ''' <summary>
    ''' 返回指定凭证类别 和 编号 的所有分录
    ''' </summary>
    ''' <param name="vMonth"></param>
    ''' <param name="vType"></param>
    ''' <param name="vId"></param>
    ''' <returns></returns>
    Public Function GetEntries(ByVal vMonth As Integer, ByVal vType As String, ByVal vId As Integer) As List(Of RjClsVoucher)

        Dim tmpRetList As List(Of RjClsVoucher) = New List(Of RjClsVoucher)()

        Try

            Dim tmpDb As ClsDbAccessMdb = GetConnection()
            Dim tmpSql As String = "Select * from  [" & Me.AppTableName & "] "
            tmpSql &= " Where [月份]=@Month and [凭证类别]=@Type and [凭证号]=@Number "
            Dim tmpParas(2) As OleDb.OleDbParameter
            tmpParas(0) = New OleDb.OleDbParameter("@Month", OleDb.OleDbType.Integer)
            tmpParas(1) = New OleDb.OleDbParameter("@Type", OleDb.OleDbType.VarWChar)
            tmpParas(2) = New OleDb.OleDbParameter("@Number", OleDb.OleDbType.Integer)

            tmpParas(0).Value = vMonth
            tmpParas(1).Value = vType
            tmpParas(2).Value = vId

            '打开数据库
            tmpDb.DbOpen()

            '执行插入
            tmpRetList = CType(ConvertRecordToClass(tmpDb.ExecuteSqlReturnDataSet(tmpSql, tmpParas)), List(Of RjClsVoucher))

            '关闭数据库
            tmpDb.DbClose()

        Catch ex As OleDb.OleDbException

            mErrorCreater = "ClsTAccountDbAccess - GetAllMonthInVoucher"
            mErrorMessage = ex.Message
            mErrorNumber = ex.ErrorCode

        End Try

        Return tmpRetList

    End Function

    ''' <summary>
    ''' 返回有凭证的所有月份、类别、凭证号的集合
    ''' </summary>
    ''' <returns></returns>
    Public Function GetAllMonthInVoucher() As List(Of RjClsVoucherBody)

        Dim tmpRetList As List(Of RjClsVoucherBody) = New List(Of RjClsVoucherBody)()

        Try

            Dim tmpDb As ClsDbAccessMdb = GetConnection()
            Dim tmpSql As String = "SELECT distinct [月份],[凭证类别],[凭证号],[附件数] FROM [" & Me.AppTableName & "] Order by [月份]"

            '打开数据库
            tmpDb.DbOpen()

            '执行插入
            Dim reader As OleDb.OleDbDataReader = tmpDb.ExecuteSqlReturnDataSet(tmpSql, New OleDb.OleDbParameter() {})

            While reader.Read()
                Dim tmpBody As RjClsVoucherBody = New RjClsVoucherBody()

                With tmpBody
                    .Month = reader.GetInt32(0)
                    .Type = reader.GetString(1)
                    .Number = reader.GetInt32(2)
                    .Attachment = reader.GetInt32(3)
                End With

                tmpRetList.Add(tmpBody)
            End While

            '关闭数据库
            tmpDb.DbClose()

        Catch ex As OleDb.OleDbException

            mErrorCreater = "ClsTAccountDbAccess - GetAllMonthInVoucher"
            mErrorMessage = ex.Message
            mErrorNumber = ex.ErrorCode

        End Try

        Return tmpRetList

    End Function


    ''' <summary>
    ''' 返回凭证分录数量
    ''' </summary>
    ''' <returns></returns>
    Public Function GetVoucherEntryCount() As Integer

        Dim tmpRetInt As Integer

        Try
            Dim tmpDb As ClsDbAccessMdb = GetConnection()
            Dim tmpSql As String = "Select Count(*) as VoucherCount from  [" & Me.AppTableName & "] "

            '打开数据库
            tmpDb.DbOpen()

            '执行插入
            tmpRetInt = CInt(tmpDb.ExecuteSqlReturnUniqueField(tmpSql, New OleDb.OleDbParameter() {}))

            '关闭数据库
            tmpDb.DbClose()

        Catch ex As OleDb.OleDbException

            mErrorCreater = "ClsTAccountDbAccess - GetVoucherEntryCount"
            mErrorMessage = ex.Message
            mErrorNumber = ex.ErrorCode

        End Try

        Return tmpRetInt

    End Function

    Public Overrides Function ConvertRecordToClass(reader As OleDb.OleDbDataReader) As Object


        If reader Is Nothing Then
            Return Nothing
        End If

        Try

            Dim tmpList As List(Of RjClsVoucher) = New List(Of RjClsVoucher)
            Dim tmpCls As RjClsVoucher

            While reader.Read()

                tmpCls = New RjClsVoucher()

                With tmpCls

                    .Entry = ConvertDbNullInteger(reader(0)) 'integer '分录  '0
                    .Month = ConvertDbNullInteger(reader(1)) 'integer '月份  '1
                    .Date = ConvertDbNullInteger(reader(2)) 'integer '日期  '2
                    .VoucherGroup = ConvertDbNullString(reader(3)) 'string '凭证类别  '3
                    .VoucherNumber = ConvertDbNullInteger(reader(4)) 'integer '凭证号  '4
                    .Exp = ConvertDbNullString(reader(5)) 'string '摘要  '5
                    .AcctCode = ConvertDbNullString(reader(6)) 'string '科目  '6
                    .AcctName = ConvertDbNullString(reader(7)) 'string '科目名称  '7
                    .Debit = ConvertDbNullDecimal(reader(8)) 'decimal '借  '8
                    .Credit = ConvertDbNullDecimal(reader(9)) 'decimal '贷  '9
                    .ProjectTrans = ConvertDbNullString(reader(10)) 'string '核算  '10
                    .ProjectSepcial = ConvertDbNullString(reader(11)) 'string '专项  '11
                    .Quantity = ConvertDbNullSingle(reader(12)) 'single '数量  '12
                    .Currency = ConvertDbNullDecimal(reader(13)) 'decimal '外币  '13
                    .Checked = reader.GetBoolean(14) 'boolean '对帐  '14
                    .Posted = reader.GetBoolean(15) 'boolean '记账  '15
                    .ProjectCode = ConvertDbNullString(reader(16)) 'string '项目编码  '16
                    .ProjectName = ConvertDbNullString(reader(17)) 'string '项目  '17
                    .Maker = ConvertDbNullString(reader(18)) 'string '制单  '18
                    .Checker = ConvertDbNullString(reader(19)) 'string '复核  '19
                    .Poster = ConvertDbNullString(reader(20)) 'string '记账人  '20
                    .K1 = reader.GetBoolean(21) 'boolean 'k1  '21
                    .Attachment = ConvertDbNullInteger(reader(22)) 'integer '附件数  '22

                End With

                tmpList.Add(tmpCls)

            End While

            Return tmpList

        Catch ex As Exception

            mErrorNumber = -1001
            mErrorMessage = ex.Message
            mErrorCreater = "ClsTVoucherGroupDbAccess - ConvertRecordToClass"

            Return Nothing
        End Try


    End Function
End Class
